package cn.eveo.refreshandloadmore.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import cn.eveo.refreshandloadmore.R;
import cn.eveo.refreshandloadmore.adapter.AbsListAdapter;
import cn.eveo.refreshandloadmore.view.LoadMoreView;
import cn.eveo.refreshandloadmore.view.PtrOnRefreshListener;
import cn.eveo.refreshandloadmore.view.PtrWrapListView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * A simple {@link Fragment} subclass.
 */
public class ListFragment extends Fragment {

    private View emptyView;
    private PtrWrapListView refreshLayout;
    private ListView listView;
    private AbsListAdapter adapter;
    private List<String> datas;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_list, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        emptyView = getActivity().findViewById(R.id.empty);
        refreshLayout = (PtrWrapListView) getActivity().findViewById(R.id.listview_refresh);
        listView = refreshLayout.getListView();
        datas = new ArrayList<>();
        adapter = new AbsListAdapter(getActivity(), datas);

        refreshLayout.setLoadingMoreEnabled(true);
        refreshLayout.setEmptyView(emptyView);

        listView.setAdapter(adapter);
        refreshLayout.setRefreshListener(new PtrOnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }

            @Override
            public void onLoadMore() {
                loadMoreData();
            }
        });
        refreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.autoRefresh();
            }
        }, 1000);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            }
        });

    }

    private void isEmpty() {
        if (datas.size() <= 0) {
            emptyView.setVisibility(View.VISIBLE);
        } else {
            emptyView.setVisibility(View.GONE);
        }
    }

    private void refreshData() {
        refreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                datas.clear();
                for (int i = 0; i < 10; i++) {
                    datas.add("data: " + i);
                }
                refreshLayout.onRefreshComplete();
                adapter.notifyDataSetChanged();
                isEmpty();
            }
        }, 2000);
    }

    private void loadMoreData() {
        refreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    datas.add("data: " + i);
                }
                refreshLayout.onRefreshComplete();
                adapter.notifyDataSetChanged();

                isEmpty();

            }
        }, 2000);
    }

}
